/*
 * Created by SharpDevelop.
 * User: Guy Fomi
 * Date: 2/6/2011
 * Time: 2:14 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Web.Script.Serialization;
using FileSystemDatabase;

namespace TomatoSynchronizer.Store
{
	/// <summary>
	/// Description of OrderStore.
	/// </summary>
	public class OrderStore
	{
		private Site _site;
		private HtmlWeb _client;
		
		private JsonDatabase db;
		public void PersistOrders(List<Dictionary<string,object>> Orders)
		{
			db.Write<List<Dictionary<string,object>>>(Orders,_site.Name);
		}

		public void PersistOrder(Dictionary<string,object> Order)
		{			
			db.Write<Dictionary<string,object>>(Order,Order["orders_id"].ToString());
		}
		
		public OrderStore(Site site,HtmlWeb client)
		{
			db = new JsonDatabase("sites/" + site.Name + "/Orders");
			_site = site;
			_client = client;
		}
		
		public List<Dictionary<string,object>> GetOrders()
		{
			string id = "";
			List<Dictionary<string,object>> Orders = new List<Dictionary<string,object>>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_orders&module=orders");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						Dictionary<string,object> Order = (Dictionary<string,object>)recs[i];
						id = Order["orders_id"].ToString();
						Order = GetOrder(id);
						Order["orders_id"] = id;
						Order["order_products"] = GetOrderProducts(id);
						Order["orders_status"] = GetOrderStatus(id);
						Order["gift_certificates"] = GetGiftCertificates(id);
						Orders.Add(Order);
					}									
				}
			}			
			
			return Orders;
		}
		
		private List<Dictionary<string,object>> GetOrderStatus(string OrderId)
		{
			List<Dictionary<string,object>> OrderStatus = new List<Dictionary<string,object>>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_orders_status&module=orders&orders_id=" + OrderId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						Dictionary<string,object> orderStatus = (Dictionary<string,object>)recs[i];
						OrderStatus.Add(orderStatus);												
					}									
				}
			}			
			
			return OrderStatus;
		}
		
		private Dictionary<string,object> GetOrder(string OrderId)
		{
			List<Dictionary<string,object>> entries = new List<Dictionary<string,object>>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=load_order&module=orders&orders_id=" + OrderId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "data")
				{					
					Dictionary<string,object> Order = (Dictionary<string,object>)json[key];					
					entries.Add(Order);
				}
			}			
			
			return entries[0];
		}
		
		private List<Dictionary<string,object>> GetOrderProducts(string OrderId)
		{
			List<Dictionary<string,object>> OrderProducts = new List<Dictionary<string,object>>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_orders_edit_products&module=orders&orders_id=" + OrderId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						Dictionary<string,object> OrderProduct = (Dictionary<string,object>)recs[i];
						OrderProducts.Add(OrderProduct);												
					}									
				}
			}			
			
			return OrderProducts;
		}		
		
		private List<Dictionary<string,object>> GetGiftCertificates(string OrderId)
		{
			List<Dictionary<string,object>> GiftCertificates = new List<Dictionary<string,object>>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_gift_certificates&module=orders&orders_id=" + OrderId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						Dictionary<string,object> GiftCertificate = (Dictionary<string,object>)recs[i];
						GiftCertificates.Add(GiftCertificate);												
					}									
				}
			}			
			
			return GiftCertificates;
		}
	}
}
